<template>
  <div>
    <el-dialog v-loading.lock="loading"
      class="base-dialog system-user-dialog"
      :append-to-body="true"
      title="物料库存表信息"
      align="center"
      :close-on-click-modal="false"
      :visible.sync="innerDialogFormVisible">
      <el-form ref="formData"
        class="base-pipeline-layout system-user-form"
        label-width="100px"
        :model="formData"
        :rules="formDataRules"
        status-icon>
        <el-form-item label="产品id"
          :label-width="formLabelWidth"
          prop="productId"
          :error="errorMsg.productId">
          <el-input v-model="formData.productId"
            autocomplete="off"
            type="number"></el-input>
        </el-form-item>
        <el-form-item label="产品名称"
          :label-width="formLabelWidth"
          prop="productName"
          :error="errorMsg.productName">
          <el-input v-model="formData.productName"
            autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="编号"
          :label-width="formLabelWidth"
          prop="productNum"
          :error="errorMsg.productNum">
          <el-input v-model="formData.productNum"
            autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="仓库id"
          :label-width="formLabelWidth"
          prop="storageId"
          :error="errorMsg.storageId">
          <el-input v-model="formData.storageId"
            autocomplete="off"
            type="number"></el-input>
        </el-form-item>
        <el-form-item label="仓库Name"
          :label-width="formLabelWidth"
          prop="storageName"
          :error="errorMsg.storageName">
          <el-input v-model="formData.storageName"
            autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="库区"
          :label-width="formLabelWidth"
          prop="storageAreaId"
          :error="errorMsg.storageAreaId">
          <el-input v-model="formData.storageAreaId"
            autocomplete="off"
            type="number"></el-input>
        </el-form-item>
        <el-form-item label="库区Name"
          :label-width="formLabelWidth"
          prop="storageAreaName"
          :error="errorMsg.storageAreaName">
          <el-input v-model="formData.storageAreaName"
            autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="库位"
          :label-width="formLabelWidth"
          prop="storageSpaceId"
          :error="errorMsg.storageSpaceId">
          <el-input v-model="formData.storageSpaceId"
            autocomplete="off"
            type="number"></el-input>
        </el-form-item>
        <el-form-item label="库位Name"
          :label-width="formLabelWidth"
          prop="storageSpaceName"
          :error="errorMsg.storageSpaceName">
          <el-input v-model="formData.storageSpaceName"
            autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="实际库存"
          :label-width="formLabelWidth"
          prop="actualCount"
          :error="errorMsg.actualCount">
          <el-input v-model="formData.actualCount"
            autocomplete="off"
            type="number"></el-input>
        </el-form-item>
        <el-form-item label="冻结库存"
          :label-width="formLabelWidth"
          prop="frozenCount"
          :error="errorMsg.frozenCount">
          <el-input v-model="formData.frozenCount"
            autocomplete="off"
            type="number"></el-input>
        </el-form-item>
        <el-form-item label="可用库存"
          :label-width="formLabelWidth"
          prop="availableCunt"
          :error="errorMsg.availableCunt">
          <el-input v-model="formData.availableCunt"
            autocomplete="off"
            type="number"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
        class="dialog-footer"
        align="center">
        <el-button type="primary"
          @click="onSubmit('formData')">保 存</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { create, update } from "../../../api/Material/MaterialInventory.js";

export default {
  name: "MaterialInventoryEdit",
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      loading: false,
      innerDialogFormVisible: false,
      formLabelWidth: "100px",
      initData: {
        id: 0,
        productId: 0,
        productName: "",
        productNum: "",
        storageId: 0,
        storageName: "",
        storageAreaId: 0,
        storageAreaName: "",
        storageSpaceId: 0,
        storageSpaceName: "",
        actualCount: 0,
        frozenCount: 0,
        availableCunt: 0,
      },
      formData: {
        id: 0,
        productId: 0,
        productName: "",
        productNum: "",
        storageId: 0,
        storageName: "",
        storageAreaId: 0,
        storageAreaName: "",
        storageSpaceId: 0,
        storageSpaceName: "",
        actualCount: 0,
        frozenCount: 0,
        availableCunt: 0,
      },
      formDataRules: {
        productId: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        productName: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
        productNum: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
        storageId: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        storageName: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
        storageAreaId: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        storageAreaName: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
        storageSpaceId: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        storageSpaceName: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
        actualCount: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        frozenCount: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        availableCunt: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
      },
      initErrorMsg: {
        productId: "",
        productName: "",
        productNum: "",
        storageId: "",
        storageName: "",
        storageAreaId: "",
        storageAreaName: "",
        storageSpaceId: "",
        storageSpaceName: "",
        actualCount: "",
        frozenCount: "",
        availableCunt: "",
      },
      errorMsg: {
        productId: "",
        productName: "",
        productNum: "",
        storageId: "",
        storageName: "",
        storageAreaId: "",
        storageAreaName: "",
        storageSpaceId: "",
        storageSpaceName: "",
        actualCount: "",
        frozenCount: "",
        availableCunt: "",
      },
    };
  },
  watch: {
    dialogFormVisible(val) {
      this.innerDialogFormVisible = val;
    },
    innerDialogFormVisible(val) {
      this.$emit("update:dialogFormVisible", val);
    },
  },
  methods: {
    cancel() {
      this.innerDialogFormVisible = false;
    },
    closeDialog() {
      this.$parent.getList();
      this.cancel();
    },
    onSubmit() {
      this.$refs.formData.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.errorMsg = this.Helper.deepCopy(this.initErrorMsg);
          if (this.formData.id <= 0) {
            create(this.formData)
              .then((res) => {
                if (res.code === 200) {
                  this.closeDialog();
                } else {
                  this.showValidateMsgCheck(res);
                }
                this.loading = false;
              })
              .catch(() => {
                this.loading = false;
              });
          } else {
            update(this.formData)
              .then((res) => {
                if (res.code === 200) {
                  this.closeDialog();
                } else {
                  this.showValidateMsgCheck(res);
                }
                this.loading = false;
              })
              .catch(() => {
                this.loading = false;
              });
          }
        } else {
          return false;
        }
      });
    },
    showValidateMsgCheck(res) {
      if (res.msg) {
        this.Helper.showValidateMsg("productId", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("productName", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("productNum", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("storageId", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("storageName", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("storageAreaId", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("storageAreaName", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("storageSpaceId", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("storageSpaceName", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("actualCount", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("frozenCount", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("availableCunt", this.errorMsg, res.msg);
      }
    },
  },
};
</script>
